/**
 * 
 * author:mengxn
 * date:2016年8月31日3:18:42
 */
(function(){
	
	var keyCompet = {
			/**
			 * 页面加载方法
			 * editor 1 为编辑
			 */
			keyCompet_read:function(){
                
			 var editor = window.sessionStorage.editor;
			 if(editor == 1){
				var editor_text = window.sessionStorage.editor_text;
				var editor_product =window.sessionStorage.editor_product;
				//console.log(editor_text);
				$("#key_name").val(editor_text);
				$("#key_product").val(editor_product);
			 }

		},
		/**
		 * 增加竞品方法 
		 * name
		 * product
		 */
		keyCompet_insert:function(name,product){
			var userid = window.localStorage.userid;
			var data = {
					"pk_user": userid,
					"name": name,
					"product": product,
					"enable":"Y",
			};
			var url = "/compatible/insert";
			common.interfaceAjaxFunction(url,data,this.keyCompet_successCallBack,true);
			common.showLoading();
		},
		/**
		 * 增加成功回调
		 */
		keyCompet_successCallBack:function(res){
	    	  common.hideLoading();
	    	  //console.log(res);
		      if(res.status==0){
		    	  parent.$("#key_serch").click();
		    	  parent.layer.msg('新增成功');
		    	  window.sessionStorage.removeItem('key_productId');
		    	  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
		    	  parent.layer.close(index);
		      }else{
		    	  layer.msg(res.message);
		      }
	      },
			/**
			 * 编辑方法
			 */
			key_edit:function(name,pk_product,pk){
				//console.log("当前编辑记录PK",pk);
				//console.log("壳牌产品PK",pk_product);
				var userid = window.localStorage.userid;
				//原来的竞品名称
				var editor_text = window.sessionStorage.editor_text;
				var data = {
						"pk_user": userid,
						"name"   : name,
						"pk":pk,
						"product":pk_product,
						"oldkey":editor_text
				};
				var url = "/compatible/update";
				common.interfaceAjaxFunction(url,data,this.alertsuccess,true);
				common.showLoading();
			},
			/**
			 * 编辑方法成功回调
			 */
			alertsuccess:function(res){
				      common.hideLoading();
				      //console.log(res);
				      if(res.status==0){
				    	  parent.$("#key_serch").click();
				    	  parent.layer.msg('修改成功');
				    	  window.sessionStorage.removeItem('key_productId');
				    	  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
				    	  parent.layer.close(index);
				      }else{
				    	  layer.msg(res.message);
				      }

			},
	      /**
	       * 绑定点击事件
	       */
	      
	      bindEvent :function(){
	    		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引	
	    		window.sessionStorage.layerIndex = index;
	    		/**
	    		 * 点击取消
	    		 */	
	    		$("#keyCompet_cancel").unbind().on("click",function(){
	    			window.sessionStorage.removeItem('key_productId');
	    			 parent.layer.close(index);
	    		});
	    		/**
	    		 * 点击保存
	    		 */
	    		$("#keyCompet_save").unbind().on("click",function(){
	    			var name = $('#key_name').val();
	    			var product = $('#key_product').val();
	    			if(name === ''){
	    		        parent.layer.msg('请填写商品名称');
	    		        return;
	    		    }
	    			if(product ===''){
	    		        parent.layer.msg('请选择商品品');
	    		        return;
	    		    }
	    			
                    var key_productId= window.sessionStorage.key_productId;//选择壳牌产品id
                    //console.log("壳牌产品pk",key_productId);
                    var pk_product=window.sessionStorage.pk_product;//编辑当前记录id
                    //console.log("当前记录pk",pk_product);
                    var editor = window.sessionStorage.editor;
       			    if(editor == 1){
       			    	keyCompet.key_edit(name,key_productId,pk_product);
       			    }else{
       			    	keyCompet.keyCompet_insert(name,key_productId);
       			    }
                    
	    			
	    		});
	    		
	    	  
	      },
	};

	
$(document).ready(function(){
	keyCompet.keyCompet_read();
	keyCompet.bindEvent();
	/**
	 * 点击选择跳转到选择竞品页面
	 */
	   $("#select").on("click",function(){
		   window.sessionStorage.productIds = window.sessionStorage.key_productId;
	        window.sessionStorage.productNames =$("#key_product").val();
		   parent.layer.ready(function(){ 
			  //增加编辑
			   parent.layer.open({
			    type: 2,
			    //skin: 'layui-layer-lan',
			    title: '选择壳牌产品',
			    fix: false,
			    shadeClose: true,
			    maxmin: true,
			    area: ['1000px', '500px'],
			    content: 'html/keyIndustryMaintanceChoose.html',
			  });
			});
		});

});
})();